<template>
	<view>
		<view class="show-kl" v-if="showInput == true ">
			<view class="kouling">
				<input type="number" v-model="circle_id" class="input" placeholder="请输入圈子口令">
			</view>
			<view>
				<view class="button" @tap="joinClick">加入圈子</view>
			</view>
		</view>
		<view v-if="showInput == false">
			<view class="show-name">{{circleInfo.name}}</view>
			<preview-form ref="preview" :dataList="list" v-if="list.length > 0"></preview-form>
			<view class="button" @tap="submit" v-if="showSubmit == true">立即加入</view>
		</view>
		<wechat-login ref="wechat_login"  @callback="wechatRegister"></wechat-login>
	</view>
</template>

<script>
	import previewForm from "@/components/diy-form/preview-form.vue";
	export default {
		components:{
			previewForm,
		},
		data() {
			return {
				showSubmit:false,
				showInput:false,
				circle_id:'',
				list:[],
				circleInfo:{},
				is_me:-1,
			}
		},
		onLoad(params) {
			//params.circle_id = 10006;
			if(params.circle_id)
			{
				this.circle_id = params.circle_id;
				this.getInfo();
			}else{
				this.showInput = true;
			}
		},
		methods: {
			wechatRegister(e){
				if(e.code == 1)
				{
					this.submit();
				}
			},
			async submit(){
				var that = this;
				if(that.is_me == 1)
				{
					that.$api.msg('不能加入自己创建的圈子');return ;
				}
				var checkLogin = that.$api.checkLogin(true);
				if(checkLogin == false)
				{
					that.$refs.wechat_login.saveShowStatus(true);
					return;
				}
				//this.$refs.preview.uploadFile(); //检测上传文件
				that.$api.showModal('确认加入','',async function(res){
					if(res == 'yes')
					{
						if(that.circleInfo.formlist != '')
						{
							var resData = await that.$refs.preview.getFormData(); //获取最终数据方法
							//console.log('join resData',resData);
							if(!resData || resData.length < 1)
							{
								return false;
							}
						}else{
							var resData = '';
						}
						that.$api.loadingMsg('正在加入');
						that.$http.post('circle/joinUserCircle',{
							resData,
							circle_id:that.circle_id
						}).then(res=>{
							that.$api.msg(res.msg)
							if(res.code == 1)
							{
								that.showSubmit = false;
								setTimeout(function(){
									that.$api.navbackTo();
								},2000)
							}
						})
					}
				})
				//var check = this.$refs.preview.checkFormData();  //检测填写数据是否完善
				//console.log(resData)
			},
			joinClick(){
				if(this.circle_id > 0) 
				{
					this.$api.loadingMsg('加载中');
					this.getInfo();
				}
			},
			getInfo(){
				var that = this;
				if(that.circle_id > 0) 
				{
					that.$http.post('circle/joinUserCircleInfo',{
						circle_id:that.circle_id
					}).then(res=>{
						if(res.code == 1)
						{
							var info = res.data.info;
							that.circleInfo = info;
							if(info.formlist)
							{
								that.list = JSON.parse(info.formlist)
							}
							that.showInput = false;
							that.showSubmit = true;
							that.is_me = res.data.is_me;
						}else{
							that.showInputFunc(res.msg);
						}
					})
				}else{
					that.showInputFunc('未获取到班级口令');
				}
			},
			showInputFunc(msg){
				var that = this;
				that.$api.showModal(msg,'',function(res){
					that.circle_id = '';
					that.showInput = true;
				})
			}
		}
	}
</script>

<style lang="scss">
	.show-name{
		background: #FFFFFF;
		    width: 94%;
		    margin: 20rpx 3%;
		    border-radius: 20rpx;
		    line-height: 40px;
		    height: 80rpx;
		    overflow: hidden;
			text-align: center;
			color: #999999;
	}
	.button{
		height: 80rpx;
		width: 90%;
		margin: 50rpx auto;
		background-color: $uni-btn-color;
		color: #ffffff;
		text-align: center;
		line-height: 80rpx;
		letter-spacing: 2rpx;
		border-radius: 30rpx;
	}
	.show-kl{
		.kouling{
			width: 100%;
			height: 100rpx;
			background: #ffffff;
			margin: 20rpx 0;
			.input{
				width: 100%;
				padding-left: 40rpx;
				height: 100rpx;
			}
		}
	}
	
</style>
